package com.dl.exam.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dl.exam.annotation.MethodInfo;
import com.dl.exam.entity.User;
import com.dl.exam.service.CommonService;
import com.dl.exam.util.CaptchaUtils;
import com.dl.exam.util.result.Info;
import com.dl.exam.util.result.Result;
import com.dl.exam.util.result.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author DL
 * @create 2022-04-21 20:33
 */
@Slf4j
@RestController
@RequestMapping("/info")
public class InfoController {

    @Resource
    private CommonService commonService;

    @MethodInfo
    @GetMapping("/getRole")
    private Result getRole()  {
        List<String> roleList = StpUtil.getRoleList();
        if (roleList != null){
            // return Result.ok().data("roles", roleList).data("name", StpUtil.getSession().get("name"));
            return Result.ok().data("roles", roleList);
        }else {
            return Result.error("失败");
        }
    }

    // 修改密码
    @MethodInfo
    @PostMapping("/modify")
    private Result modifyPassword(String password, String newPassword)  {
        //判断密码是否修改成功
        boolean modify = commonService.modifyPassword(newPassword, StpUtil.getLoginIdAsString(), password);
        if (modify){
            return Result.ok("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }

    // 查询用户通用简单信息 姓名、邮箱、电话
    @MethodInfo
    @PostMapping("/getBasic")
    private Result getUserBasicInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = commonService.getUserBasicInfo(StpUtil.getLoginIdAsString());
        if (user != null){
            return Result.ok("成功").data("user", user);
        }else {
            return Result.error("失败");
        }
    }

    // 更新用户通用简单信息 姓名、邮箱、电话
    //region 修改密码
    @MethodInfo
    @PostMapping("/modifyBasic")
    private Result modifyUserBasicInfo(@RequestBody JSONObject jsonObject) {
        boolean modifyUserBasicInfo = commonService.modifyUserBasicInfo(StpUtil.getLoginIdAsString(), jsonObject);
        if (modifyUserBasicInfo){
            return Result.ok("成功");
        }else {
            return Result.error("失败");
        }
    }
}
